package org.example.exceptions;


import lombok.Data;
import org.example.enums.ErrorCodeEnum;
import org.example.utils.StringBulidUtils;

@Data
public class BusinessException extends RuntimeException {
    /**
     * 异常信息
     */
    private String errorMsg;

    /**
     * 异常编码
     */
    private Integer errorCode;

    public BusinessException(Integer errorCode, String errorMsg){
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
    }

    @Override
    public  Throwable fillInStackTrace() {
        return this;
    }

    public BusinessException(ErrorCodeEnum errorCodeEnum){
        this(errorCodeEnum.getCode(), errorCodeEnum.getDesc());
    }

    public BusinessException(ErrorCodeEnum errorCodeEnum, Throwable e){
        super(errorCodeEnum.getDesc(), e);
        this.errorCode = errorCodeEnum.getCode();
        this.errorMsg = errorCodeEnum.getDesc();
    }

    public BusinessException(ErrorCodeEnum errorCodeEnum, Throwable e, Object... args){
        super(errorCodeEnum.getDesc(), e);
        this.errorCode = errorCodeEnum.getCode();
        this.errorMsg = StringBulidUtils.replaceAllBrace(errorCodeEnum.getDesc(), args);
    }

    public BusinessException(ErrorCodeEnum errorCodeEnum, Object... args){
        this(errorCodeEnum.getCode(), StringBulidUtils.replaceAllBrace(errorCodeEnum.getDesc(), args));
    }
}
